<?php

namespace App\Model;

use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\AbstractModel;

/**
 * UserModel
 * Class UserModel
 * Create With ClassGeneration
 * @property int $id // id
 * @property string $name // 姓名
 */
class UserModel extends BaseModel
{
	protected $tableName = 'user';


    /** hasOne 一对一关联  可拓展自定义append
     * @return array|mixed|null
     * @throws \Throwable
     * *    author 韩天尊
     */
    public function relation()
    {
        return $this->hasOne(RelationModel::class, function(QueryBuilder $query){
            //$query->where('user_id', $this->id);
            //return $query;
        }, "id", "user_id");
	}

    /** hasMany 一对多关联  可拓展自定义append
     * @return array|mixed|null
     * *    author 韩天尊
     */
    public function phones(){
        return $this->hasMany(PhoneModel::class, function (QueryBuilder $builder){
            $builder->orderBy("id","DESC");
        }, 'id', 'user_id');
    }


	public function addData(string $name): self
	{
		$data = [
		    'name'=>$name,
		];
		$model = new self($data);
		$model->save();
		return $model;
	}
}

